/**
 * 关于对于request的异步请求的封装
 */
export class Request {
	constructor(arg) {
		this.resDomain = ""; //资源地址
		this.Domain = "@/pages/education/static/data_json"; //接口地址
		this.networkStatus = true; //网络状态的存储
		this.interface_url = {
			edu_index: "/index.json", //全年龄段教育首页
		};
		this.timeout = 30000; //请求超时
		//获取网络状态
		uni.getNetworkType({
			success: function(res) {
				(res.networkType == 'none') ? (this.networkStatus = false) : "";
			}
		});
	}
	/**
	 * 消失提示
	 * @param {Object} content
	 */
	showToast(content) {
		uni.hideLoading();
		uni.showToast({
			title: content,
			icon: "none"
		});

	}
	getInfo() {
			return new Promise((resolve, reject) => {
				uni.getSystemInfo({
					success: (e) => {
						let StatusBar = "";
						let CustomBar = ";"
						// #ifndef MP
						StatusBar = e.statusBarHeight;
						if (e.platform == 'android') {
							CustomBar = e.statusBarHeight + 50;
						} else {
							CustomBar = e.statusBarHeight + 45;
						};
						// #endif

						// #ifdef MP-WEIXIN
						StatusBar = e.statusBarHeight;
						let custom = wx.getMenuButtonBoundingClientRect();

						CustomBar = custom.bottom + custom.top - e.statusBarHeight;
						// #endif		

						// #ifdef MP-ALIPAY
						StatusBar = e.statusBarHeight;
						CustomBar = e.statusBarHeight + e.titleBarHeight;
						// #endif

						let dat = {
							StatusBar: StatusBar,
							winW:e.windowWidth,
							winH:e.windowHeight,
							CustomBar: CustomBar
						}
						resolve(dat);
					}
				});
			})

		}

		/**
		 * 网络断开的提示
		 */
		tishi() {
			uni.showModal({
				title: "提示",
				content: "网络似乎断开了",
				showCancel: false,
				success: (e) => {
					console.log(e);
				}
			})
		}

	/**
	 * 关于异步请求的封装 需登录信息附带
	 * @param {Object} url 请求的地址
	 * @param {Object} type 请求的类型
	 * @param {Object} data  请求的参数
	 */
	request(url, type, data, ishow) {

		let token = uni.getStorageSync('token');
		let header = {
			'Content-Type': 'application/json',
			'token': token
		};
		return new Promise((resolve, reject) => {
			uni.request({
				url: this.Domain + url,
				method: type,
				data: data,
				timeout: this.timeout,
				header: header,

				success: res => {
					resolve(res.data);
				},
				fail: (e) => {
					reject(e);
					if (ishow) {
						this.showToast("请求超时");
					}

				},
				complete: () => {}
			});

		});

	}
	/**
	 * 普通接口的数据请求
	 * 无须处于登录状态
	 * @param {Object} url
	 * @param {Object} type
	 * @param {Object} data
	 */
	common(url, type, data) {
		return new Promise((resolve, reject) => {
			uni.request({
				url: this.Domain + url,
				method: type,
				data: data,
				header: {
					'Content-Type': 'application/json'
				},
				success: res => {
					resolve(res.data);
				},
				fail: (e) => {
					reject(e);
				},
				complete: (e) => {
					console.log(e)
				}
			});

		});
	}

	/**
	 *  "服务器开小差了";
	 */
	server_err() {
		return "服务器开小差了";

	}


}
